/**
 * 管理初始化
 */
var Teacher = {
    id: "TeacherTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Teacher.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '教师', field: 'teacherId', visible: false, align: 'center', valign: 'middle'},
            {title: '教师ID', field: 'accountId', visible: true, align: 'center', valign: 'middle'},
            {title: '教师名称', field: 'teacherName', visible: true, align: 'center', valign: 'middle'},
            {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
            {title: '省code', field: 'provinceCode', visible: false, align: 'center', valign: 'middle'},
            {title: '市code', field: 'cityCode', visible: false, align: 'center', valign: 'middle'},
            {title: '区县code', field: 'areaCode', visible: false, align: 'center', valign: 'middle'},
            {title: '就职省code', field: 'orgProvinceCode', visible: false, align: 'center', valign: 'middle'},
            {title: '就职省', field: 'orgProvinceName', visible: true, align: 'center', valign: 'middle'},
            {title: '就职市code', field: 'orgCityCode', visible: false, align: 'center', valign: 'middle'},
            {title: '就职市', field: 'orgCityName', visible: true, align: 'center', valign: 'middle'},
            {title: '就职区县code', field: 'orgAreaCode', visible: false, align: 'center', valign: 'middle'},
            {title: '就职区县', field: 'orgAreaName', visible: true, align: 'center', valign: 'middle'},
            {title: '学校id', field: 'schoolId', visible: false, align: 'center', valign: 'middle'},
            {title: '就职学校', field: 'schoolName', visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = '';
                if (value == "0") {
                    text = "下架";
                } else if (value == "1") {
                    text = "上架";
                }
                return text;
            }},
            {title: '站点', field: 'siteId', visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = '';
                if (value == "001") {
                    text = "万柏林";
                } else if (value == "002") {
                    text = "运城";
                }else if (value == "003") {
                    text = "贵州";
                }else if (value == "004") {
                    text = "科协";
                }
                return text;
            }},
            {title: '创建人id', field: 'createUserId', visible: false, align: 'center', valign: 'middle'},
            {title: '修改人id', field: 'updateUserId', visible: false, align: 'center', valign: 'middle'},
            {title: '修改时间', field: 'updateTime', visible: false, align: 'center', valign: 'middle'},
            {title: '是否删除 0-否 1-是', field: 'beDelete', visible: false, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = '';
                if (value == "0") {
                    text = "否";
                } else if (value == "1") {
                    text = "是";
                }
                return text;
            }},
            {title: '排序', field: 'num', visible: false, align: 'center', valign: 'middle'},
            {title: '名师图片', field: 'imageUrl', visible: false, align: 'center', valign: 'middle'},
            {title: '名师简介', field: 'teacherDesc', visible: false, align: 'center', valign: 'middle'},
            {title: '置顶 ', field: 'beTop', visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = '已置顶';
                if (value == "1") {
                    text = "已置顶";
                } else if (value == "0") {
                    text = "未置顶";
                } 
                return text;
            }}
    ];
};

/**
 * 检查是否选中
 */
Teacher.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Teacher.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加
 */
Teacher.openAddTeacher = function () {
    var index = layer.open({
        type: 2,
        title: '添加',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/teacher/teacher_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看详情
 */
Teacher.openTeacherDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '编辑',
            area: ['1000px', '520px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/teacher/teacher_update/' + Teacher.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除
 */
Teacher.delete = function () {
    if (this.check()) {
    	var operation = function(){
	        var ajax = new $ax(Feng.ctxPath + "/teacher/delete", function (data) {
	            Feng.success("删除成功!");
	            Teacher.table.refresh();
	        }, function (data) {
	            Feng.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("id",Teacher.seItem.id);
	        ajax.set("teacherId",Teacher.seItem.teacherId);
	        ajax.start();
    	};
    	Feng.confirm("是否删除?",operation);
    }
};

/**
 * 下架
 */
Teacher.downOrUp = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/teacher/downOrUp", function (data) {
            Feng.success("操作成功!");
            Teacher.table.refresh();
        }, function (data) {
            Feng.error("操作失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",Teacher.seItem.id);
        ajax.start();
    }
};

/**
 * 置顶
 */
Teacher.topOrNo = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/teacher/topOrNo", function (data) {
            Feng.success("操作成功!");
            Teacher.table.refresh();
        }, function (data) {
            Feng.error("操作失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",Teacher.seItem.id);
        ajax.start();
    }
};

/**
 * 查询列表
 */
Teacher.search = function () {
    var queryData = {};
    queryData['teacherName'] = $("#teacherName").val();
    //queryData['teacherId'] = $("#teacherId").val();
    queryData['accountId'] = $("#accountId").val();
    queryData['orgProvinceCode'] = $("#orgProvinceCode").val();
    queryData['orgCityCode'] = $("#orgCityCode").val();
    queryData['orgAreaCode'] = $("#orgAreaCode").val();
    queryData['schoolId'] = $("#schoolId").val();
    Teacher.table.refresh({query: queryData});
};

/**
 * 重置
 */
Teacher.resetSearch = function () {
    $("#teacherName").val("");
    $("#accountId").val("");
    $("#orgProvinceCode").val("");
    $("#orgCityCode").val("");
    $("#orgAreaCode").val("");
    $("#schoolId").val("");
    Teacher.search();
}

/**
 * 获取市
 */
function getCityList(orgProvinceCode) {
        var ajax = new $ax(Feng.ctxPath + "/clmRoom/getCityList", function (resultData) {
        	jQuery("#orgCityCode").html("");
        	if(resultData instanceof Array){
        		jQuery("#orgCityCode").append("<option value='' ></option>");
                for (key in resultData){
                    var name=resultData[key].cityName;
                    var code=resultData[key].cityCode;
                     jQuery("#orgCityCode").append("<option value='"+code+"' >"+name+"</option>");
                }
                //getDiscList(resultData[0].orgCityCode);
			}else{
	             var name=resultData.cityName;
	             var code=resultData.cityCode;
				 jQuery("#orgCityCode").append("<option value='"+code+"' >"+name+"</option>");
			}
        	
        }, function (data) {
        	jQuery("#orgCityCode").append("<option></option>");
            jQuery("#orgAreaCode").html("<option></option>");
            jQuery("#schoolId").html("<option></option>");
        });
        ajax.set("provinceCode",orgProvinceCode);
        ajax.start();
};

/**
 * 获取区县
 */
function getDiscList(orgCityCode) {
        var ajax = new $ax(Feng.ctxPath + "/clmRoom/getDiscList", function (resultData) {
        	jQuery("#orgAreaCode").html("");
        	if(resultData instanceof Array){
        		jQuery("#orgAreaCode").append("<option value='' ></option>");
                for (key in resultData){
                    var name=resultData[key].discName;
                    var code=resultData[key].discCode;
                     jQuery("#orgAreaCode").append("<option value='"+code+"' >"+name+"</option>");
                }
               // getSchoolList(resultData[0].discCode,"2");
			}else{
	             var name=resultData.discName;
	             var code=resultData.discCode;
				 jQuery("#orgAreaCode").append("<option value='"+code+"' >"+name+"</option>");
			}
        	
        }, function (data) {
            jQuery("#orgAreaCode").html("<option></option>");
            jQuery("#schoolId").html("<option></option>");
        });
        ajax.set("cityCode",orgCityCode);
        ajax.start();
};

/**
 * 获取学校
 */
function getSchoolList(code,type) {
        var ajax = new $ax(Feng.ctxPath + "/clmRoom/getSchoolList", function (resultData) {
        	jQuery("#schoolId").html("");
        	if(resultData instanceof Array){
        		jQuery("#schoolId").append("<option value='' ></option>");
                for (key in resultData){
                    var name=resultData[key].schoolName;
                    var code=resultData[key].schoolId;
                     jQuery("#schoolId").append("<option value='"+code+"' >"+name+"</option>");
                }
			}else{
	             var name=resultData.schoolName;
	             var code=resultData.schoolId;
				 jQuery("#schoolId").append("<option value='"+code+"' >"+name+"</option>");
			}
        	
        }, function (data) {
            jQuery("#schoolId").html("<option></option>");
        });
        ajax.set("code",code);
        ajax.set("type",type);
        ajax.start();
};

//省变更事件
$("#orgProvinceCode").bind("change",function () { 
	getCityList(this.value)
	if(this.value!=""){
		getSchoolList(this.value,"0");
	}
	
 })
 
 //市变更事件
$("#orgCityCode").bind("change",function () { 
	getDiscList(this.value)
	if(this.value!=""){
		getSchoolList(this.value,"1");
	}
	
 })
 
 //区县变更事件
$("#orgAreaCode").bind("change",function () { 
	if(this.value!=""){
		getSchoolList(this.value,"2");
	}
	
 })



$(function () {
    var defaultColunms = Teacher.initColumn();
    var table = new BSTable(Teacher.id, "/teacher/list", defaultColunms);
    table.setPaginationType("client");
    Teacher.table = table.init();
});
